#ifndef MYMQTT_H
#define MYMQTT_H

#include <QObject>
#include "QtMqtt/QMqttClient"
#include <QTimer>


static QString VIDEO_TOPIC = "video/#";
static QString SPECTR_TOPIC = "spectr/#";
static QString LORA_TOPIC = "lora/#";

enum class TOPICS {
  SPECTR,
  NONE = 100
};

static QMap<QString, TOPICS> TOPICS_MAP = {{"spectr", TOPICS::SPECTR}};

class MyMqtt : public QObject
{
    Q_OBJECT
public:
    //QString hst - хост, имя или IP,
    //quint16 prt - порт,
    //QString usr - имя пользователя,
    //QString pass - пароль
    explicit MyMqtt(QString hst, quint16 prt, QString usr, QString pass, QObject *parent = nullptr);
    ~MyMqtt(){};

private:
    QString userName;
    QString userPassword;
    QString hostName;
    quint16 port;

    QList<float> arrayData;

    QMqttClient *mqttClient;
    // RcvParserMQTT *rcvParser;
    QTimer *tryConnecting;

    void initMqttClient();

    //всопмогательные фун-ции
    TOPICS getTopicsNum(QString topicReceived);
    //QByteArray data - 4 bytes
    float convertByteArrayToFloat(QByteArray data);

    //парсинг
    void parseArrayData(const QByteArray &message);

private slots:
    void updateStateChange(QMqttClient::ClientState state);
    void setSubscription(QString &topic);
    void brokerDisconnected();
    void keepAliveChange();
    void sendPing();

public slots:
    void connectToBroker();
    void publishData(QString &topic, QByteArray &data);
    void getMessage(const QByteArray &message, const QMqttTopicName &topic = QMqttTopicName());

signals:
    // void newSettingsReceived(DETECTOR_MODE mode, const QByteArray &message);
    // void newModeReceived(DETECTOR_MODE mode);
    void arrayDataReceived(QList<float> &newData);
};

#endif // MYMQTT_H
